package cn.com.dhcc.turbo.service.queue.tlq.listener;

import java.util.Date;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.threadpoolhandler.ThreadPool;
import cn.com.dhcc.app.core.handler.tlqhandler.MqMessageListener;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.util.BeanMapper;
import cn.com.dhcc.app.pub.core.consts.AppConst;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.turbo.entity.api.RouteLog;
import cn.com.dhcc.turbo.entity.exch.CrossNetTmpCmData;
import cn.com.dhcc.turbo.entity.exch.QueueMsgWrapper;
import cn.com.dhcc.turbo.entity.exch.QueueMsgWrapper.ExchageLifecycle;
import cn.com.dhcc.turbo.entity.exch.SendCmData;
import cn.com.dhcc.turbo.service.exch.cmdata.CmDataExchangeLifecycle;

/**
 * 本地队列监听器
 * @author Administrator
 *
 */
@Component
public class TlqLqMessageListener extends MqMessageListener {
	@SuppressWarnings("deprecation")
	private static Logger logger = Log.getExistLogger(LOGTYPE.TLQ);
	@Autowired
	@Qualifier(AppIocBean.CM_DATA_EXCHANGE_LIFECYCLE_BEAN)
	private CmDataExchangeLifecycle cmDataExchageLifecycle;

//	private static final String THREAD_POOL_NAME = "TlqLqMessageListener";
	/*static{
		//创建线程池
		ThreadPool.createPool(THREAD_POOL_NAME, KernelParam.jmsThreadPoolMinSize, KernelParam.jmsThreadPoolMaxSize, Integer.MAX_VALUE);	
	}*/
	
	@Override
	public void onMessage(Message message) {
		try {
//			System.out.println("I am a Listener,he he ************************");
			startConsumeMsg(message);
		} catch (ServiceException e) {
			logger.error("JMS消息接收异常", e);
		}
	}
	/**
	 * 多线程消费
	 * @param message
	 * @throws ServiceException
	 */
	private void startConsumeMsg(final Message message) throws ServiceException{
		if (message instanceof ObjectMessage) {
			try {
				Object obj = ((ObjectMessage) message).getObject();
				if (obj instanceof QueueMsgWrapper) {
					final QueueMsgWrapper queueMsgWrapper = (QueueMsgWrapper) obj;
					ThreadPool.execute(queueMsgWrapper.getExchageLifecycle().name(), new Runnable() {
						@Override
						public void run() {
							consumeMsg(message);
						}
					});
				}
				
			} catch (JMSException e) {
				logger.error(e);
			}
		}
	}
    private void consumeMsg(Message message){
    	if (message instanceof ObjectMessage) {
			try {
				Object obj = ((ObjectMessage) message).getObject();
				if (obj instanceof QueueMsgWrapper) {
					QueueMsgWrapper queueMsgWrapper = (QueueMsgWrapper) obj;
					logger.info("JMS接收消息：queueName[" + queueMsgWrapper.getQueueName()+"],ExchageLifecycle["+queueMsgWrapper.getExchageLifecycle().getDesc()+"]");
					if (ExchageLifecycle.remoteStore.equals(queueMsgWrapper.getExchageLifecycle())) {
						SendCmData sendCmData = (SendCmData) queueMsgWrapper.getData();
						String msg = "传输管道[TlqTransportPipeline]接收数据成功";
						RouteLog.setRemotePipeline(sendCmData, AppConst.YES, msg, AppConst.dtFormat.format(new Date()));
						cmDataExchageLifecycle.remoteStore(sendCmData);
					}else if(ExchageLifecycle.remoteStoreTmp.equals(queueMsgWrapper.getExchageLifecycle())){
						SendCmData sendCmData = (SendCmData) queueMsgWrapper.getData();
						String msg = "传输管道[TlqTransportPipeline]接收数据成功";
						RouteLog.setInnerRecvPipeline(sendCmData, AppConst.YES, msg, AppConst.dtFormat.format(new Date()));
						cmDataExchageLifecycle.remoteStore(sendCmData);
					}else
					if (ExchageLifecycle.feedbackSendStatusAndRoutelog.equals(queueMsgWrapper.getExchageLifecycle())) {
						cmDataExchageLifecycle.updateSendStatusAndRoutelog((SendCmData) queueMsgWrapper.getData());
					}else
					if (ExchageLifecycle.feedbackSendStatusAndRoutelogToTmp.equals(queueMsgWrapper.getExchageLifecycle())) {
						SendCmData sendCmData = (SendCmData) queueMsgWrapper.getData();
						CrossNetTmpCmData tempData=new CrossNetTmpCmData();
						BeanMapper.copy(sendCmData, tempData);
						cmDataExchageLifecycle.updateSendStatusAndRoutelog(tempData);
					}else
					if (ExchageLifecycle.feedbackNotifyStatusAndRoutelogTmp.equals(queueMsgWrapper.getExchageLifecycle())) {
						SendCmData sendCmData = (SendCmData) queueMsgWrapper.getData();
						CrossNetTmpCmData tempData=new CrossNetTmpCmData();
						BeanMapper.copy(sendCmData, tempData);
						cmDataExchageLifecycle.updateNotifyStatusAndRoutelog(tempData);
					}else
					if (ExchageLifecycle.feedbackNotifyStatusAndRoutelog.equals(queueMsgWrapper.getExchageLifecycle())) {
						cmDataExchageLifecycle.updateNotifyStatusAndRoutelog((SendCmData) queueMsgWrapper.getData());
					}else
					//跨网传输
					if (ExchageLifecycle.gapEntrepot.equals(queueMsgWrapper.getExchageLifecycle())) {
						SendCmData sendCmData = (SendCmData) queueMsgWrapper.getData();
						String msg = "传输管道[TlqTransportPipeline]接收数据成功";
					    RouteLog.setOuterRecvPipeline(sendCmData, AppConst.YES, msg, AppConst.dtFormat.format(new Date()));
						cmDataExchageLifecycle.outerEntrepotStore(sendCmData);
					}
				} else {
					logger.error("JMS消息接收异常:Unknown message Type [ " + message.getClass() + " ]");
				}
			} catch (Exception e) {
				logger.error("JMS消息接收异常", e);
			}
		} else {
			//System.out.println(">>>>>>>>>>>>>>Message has consume the BusinessMessage:" + message);
		}
    }
	
}
